using System.Web.Mvc;
using good1.ApplicationServices.Interface;
using good1.Core;
using good1.Core.Enums;
using good1.Web.Controllers.Attributes;
using good1.Web.Controllers.Infrastructure;
using good1.Web.Controllers.Infrastructure.Cookies;
using SharpArch.Core.PersistenceSupport;
using System.Collections.Generic;
using SharpArch.Web.NHibernate;
using SharpArch.Web.CommonValidator;
using SharpArch.Core;

namespace good1.Web.Controllers
{
    [AuthorizeAttributeCustom(RoleRequired = GoodRoles.Nimda)]
    public class CountriesController : BaseController
    {
        public CountriesController(IRepository<Country> countryRepository, ISecurityContextService securityContext): base(securityContext) {
            Check.Require(countryRepository != null, "countryRepository may not be null");

            this.countryRepository = countryRepository;
        }

        [Transaction]
        public ActionResult Index() {
            IList<Country> countries = countryRepository.GetAll();
            return View(countries);
        }

        [Transaction]
        public ActionResult Show(int id) {
            Country country = countryRepository.Get(id);
            return View(country);
        }

        [Authorize]
        public ActionResult Create() {
            return View();
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(Country country) {
            if (country.IsValid()) {
                countryRepository.SaveOrUpdate(country);

                TempData["message"] = "The country was successfully created.";
                return RedirectToAction("Index");
            }

            MvcValidationAdapter.TransferValidationMessagesTo(ViewData.ModelState,
                country.ValidationResults());
            return View();
        }

        [Transaction]
        public ActionResult Edit(int id) {
            Country country = countryRepository.Get(id);
            return View(country);
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, [ModelBinder(typeof(DefaultModelBinder))] Country country) {
            Country countryToUpdate = countryRepository.Get(id);
            TransferFormValuesTo(countryToUpdate, country);

            if (countryToUpdate.IsValid()) {
                TempData["message"] = "The country was successfully updated.";
                return RedirectToAction("Index");
            }
            else {
                countryRepository.DbContext.RollbackTransaction();
                MvcValidationAdapter.TransferValidationMessagesTo(ViewData.ModelState, 
                    countryToUpdate.ValidationResults());
                return View(countryToUpdate);
            }
        }

        private void TransferFormValuesTo(Country countryToUpdate, Country countryFromForm) {
			countryToUpdate.Iso3 = countryFromForm.Iso3;
			countryToUpdate.NumericalCode = countryFromForm.NumericalCode;
			countryToUpdate.CountryName = countryFromForm.CountryName;
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Delete(int id) {
            string resultMessage = "The country was successfully deleted.";
            Country countryToDelete = countryRepository.Get(id);

            if (countryToDelete != null) {
                countryRepository.Delete(countryToDelete);

                try {
                    countryRepository.DbContext.CommitChanges();
                }
                catch {
                    resultMessage = "A problem was encountered preventing the country from being deleted. " +
						"Another item likely depends on this country.";
                    countryRepository.DbContext.RollbackTransaction();
                }
            }
            else {
                resultMessage = "The country could not be found for deletion. It may already have been deleted.";
            }

            TempData["Message"] = resultMessage;
            return RedirectToAction("Index");
        }

        private readonly IRepository<Country> countryRepository;
    }
}
